package controllers

import (
	"encoding/json"
	"ssims/models"
	"ssims/pkg/app"
	"ssims/pkg/e"
	"ssims/services"
	"time"

	"github.com/gin-gonic/gin"
	"github.com/unknwon/com"
)

func GetStaff(c *gin.Context) {
	page := -1
	if arg := c.Query("page"); arg != "" {
		page = com.StrTo(arg).MustInt()
	}
	limit := -1
	if arg := c.Query("limit"); arg != "" {
		limit = com.StrTo(arg).MustInt()
	}
	searchName := ""
	if arg := c.Query("searchName"); arg != "" {
		searchName = arg
	}
	staffParam := map[string]interface{}{
		"page":       page,
		"limit":      limit,
		"searchName": searchName,
	}
	err, info, total := services.GetAllStaff(staffParam)
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, map[string]interface{}{"value": info, "total": total}, "查询成功")
}

func DelStaff(c *gin.Context) {
	id := -1
	if arg := c.Query("id"); arg != "" {
		id = com.StrTo(arg).MustInt()
	}
	if id == -1 {
		app.INFO(c, 30001, "参数错误")
		return
	}
	err2, staff := services.GetStaffById(id)
	if err2 != nil {
		app.Error(c, e.ERROR, err2, err2.Error())
		return
	}
	if staff.StaffLevel == 2 {
		app.INFO(c, 30000, "管理员用户不能删除")
		return
	}
	err := services.DelStaff(id)
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, map[string]interface{}{}, "删除成功")
}

func UpdateStaff(c *gin.Context) {
	b, _ := c.GetRawData()
	var m map[string]string
	_ = json.Unmarshal(b, &m)
	if m["id"] == "" {
		app.INFO(c, 30000, "参数非法")
		return
	}
	id := -1
	id = com.StrTo(m["id"]).MustInt()
	staffName := m["staffName"]
	staffPassword := m["staffPassword"]
	staffLevel := 0
	staffLevel = com.StrTo(m["staffLevel"]).MustInt()
	staffRemarks := m["staffRemarks"]
	err := services.UpdateStaff(models.Staff{StaffId: id, StaffName: staffName, StaffPassword: staffPassword, StaffLevel: staffLevel, StaffRemarks: staffRemarks, Isdel: false, UpdateTime: time.Now()})
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, map[string]interface{}{}, "更新成功")
}

func AddStaff(c *gin.Context) {
	b, _ := c.GetRawData()
	var m map[string]string
	_ = json.Unmarshal(b, &m)
	staffName := m["staffName"]
	staffPassword := m["staffPassword"]
	staffLevel := com.StrTo(m["staffLevel"]).MustInt()
	staffRemarks := m["staffRemarks"]
	err := services.AddStaff(models.Staff{StaffId: -1, StaffName: staffName, StaffPassword: staffPassword, StaffLevel: staffLevel, StaffRemarks: staffRemarks, Isdel: false, UpdateTime: time.Now()})
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, map[string]interface{}{}, "添加成功")
}
